/**
 * @Description utils
 * @Date 2021/1/20 9:03
 * @Created by hePing
 */

/**
 * 判断有值
 * @param {Any} v
 * @returns {Boolean} 无值true，有值false
 */
export const isVal = (v) => !(v === undefined || v === null || v === '')

export const getValueType = (type = 'string') => {
  return [{ type: 'string', fn: String },
  { type: 'number', fn: Number },
  { type: 'boolean', fn: Boolean },
  { type: 'array', fn: Array },
  ].filter(o => o.type === type)[0] || {}
}

/**
 * Object格式化
 * @param {Object} o
 * @return {Object}
 */
export const objectFormat = (o) => {
  return o ? JSON.parse(JSON.stringify(o)) : o
}

/**
 * @param {Object} target
 * @param {Object} options
 * */
export const deepMerge = (target, options) => {
  Object.keys(options).forEach(k => {
    // 防止对象原型被污染，防止永无休止的循环
    if (k === '__proto__') return
    const v = options[k]
    if (!isVal(v)) return
    if (Array.isArray(v)) {
      target[k] = v
    } else if (typeof v === 'object') {
      if (!isVal(target[k])) target[k] = {}
      deepMerge(target[k], v)
    } else {
      target[k] = v
    }
  })
  return target
}
